package com.example.runnable;

import com.example.mapper.BasicMapper;
import com.example.utils.TextFileReader;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.List;
import java.util.stream.Collectors;

import static com.example.runnable.Constants.W_PATH;

@Component
@ConditionalOnProperty(name = "run-env",havingValue = "test03")
public class Test03 implements ApplicationRunner {

    @Resource
    SqlSessionFactory sqlSessionFactory;

    @Resource
    HikariDataSource hikariDataSource;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Connection connection = hikariDataSource.getConnection();
        connection.setAutoCommit(false);
//        Statement statement = connection.createStatement();
        Statement statement = connection.createStatement();
        long begin=System.currentTimeMillis();
        TextFileReader fileReader = null;
        try {
            fileReader = new TextFileReader(W_PATH,"utf-8");
            List<String> list=null;
            int i=1;
            while (fileReader.hasNext()){
                long start = System.currentTimeMillis();
                list = fileReader.readNextLines(50);
                StringBuilder sql = new StringBuilder("INSERT ALL ");
                for (String s : list) {
                    s ="'"+s.replace(",", "','")+"'";
                    sql.append("INTO T_BOND_BASIS_INFO VALUES (")
                            .append(s)
                            .append(") ");
                }
                sql.append("SELECT 1 from DUAL");
                String s = sql.toString();
//                System.out.println(s);
                statement.executeUpdate(s);
//                List<String> collect = list.stream().map(s -> "'" + s.replace(",", "','") + "'")
//                        .collect(Collectors.toList());
                if (i%4==0){
                    connection.commit();
                    long end = System.currentTimeMillis();
                    System.out.println("总耗时:"+(end-begin)/1000+"s,耗时:"+(end-start)+"ms;批次:"+(i)*50);
                }
                list.clear();
                i++;

            }
            System.out.println("耗时:"+(System.currentTimeMillis()-begin));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fileReader!=null){
                fileReader.close();
            }
            hikariDataSource.evictConnection(connection);
        }
    }
}
